package interfacesBeans;

/**
 *
 * @author Rodrigo
 */
import agenda.*;
import java.io.*;

public class Body extends javax.swing.JFrame {

    public static Usuario user;
    private static final String arquivo = "usuario.txt";

    public static TelaInicial telaInicial;
    public static Perfil perfil;
    public static Contatos contatos;
        public static VisualizarContato visualizarContato;
        public static AdicionarContato adicionarContato;
        public static EditarContato editarContato;

    /** Creates new form Body */
    public Body() {
        initComponents();

        telaInicial = new TelaInicial();
        panelBody.add(telaInicial, "TelaInicial");

        perfil = new Perfil();
        panelBody.add(perfil, "Perfil");

        contatos = new Contatos();
        panelBody.add(contatos, "Contatos");
            visualizarContato = new VisualizarContato();
            panelBody.add(visualizarContato, "VisualizarContato");

            adicionarContato = new AdicionarContato();
            panelBody.add(adicionarContato, "AdicionarContato");

            editarContato = new EditarContato();
            panelBody.add(editarContato, "EditarContato");

       Eventos eventos = new Eventos();
       panelBody.add(eventos, "Eventos");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelBotoes = new javax.swing.JPanel();
        buttonPerfil = new javax.swing.JButton();
        buttonContatos = new javax.swing.JButton();
        buttonEventos = new javax.swing.JButton();
        buttonSair = new javax.swing.JButton();
        buttonHome = new javax.swing.JButton();
        panelBody = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ppG Agenda");
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(0, 0, 800, 600));

        panelBotoes.setBackground(new java.awt.Color(255, 255, 255));
        panelBotoes.setPreferredSize(new java.awt.Dimension(800, 100));

        buttonPerfil.setBackground(new java.awt.Color(255, 255, 255));
        buttonPerfil.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/user.png"))); // NOI18N
        buttonPerfil.setText("Perfil");
        buttonPerfil.setToolTipText("Visualizar o perfil do usuário");
        buttonPerfil.setPreferredSize(new java.awt.Dimension(125, 50));
        buttonPerfil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPerfilActionPerformed(evt);
            }
        });

        buttonContatos.setBackground(new java.awt.Color(255, 255, 255));
        buttonContatos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/agenda.png"))); // NOI18N
        buttonContatos.setText("Contatos");
        buttonContatos.setPreferredSize(new java.awt.Dimension(125, 50));
        buttonContatos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonContatosActionPerformed(evt);
            }
        });

        buttonEventos.setBackground(new java.awt.Color(255, 255, 255));
        buttonEventos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/eventos.png"))); // NOI18N
        buttonEventos.setText("Eventos");
        buttonEventos.setPreferredSize(new java.awt.Dimension(125, 50));
        buttonEventos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEventosActionPerformed(evt);
            }
        });

        buttonSair.setBackground(new java.awt.Color(255, 255, 255));
        buttonSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/fechar.png"))); // NOI18N
        buttonSair.setText("Sair");
        buttonSair.setPreferredSize(new java.awt.Dimension(125, 50));
        buttonSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSairActionPerformed(evt);
            }
        });

        buttonHome.setBackground(new java.awt.Color(255, 255, 255));
        buttonHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/home.png"))); // NOI18N
        buttonHome.setText("Home");
        buttonHome.setPreferredSize(new java.awt.Dimension(125, 50));
        buttonHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonHomeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotoesLayout = new javax.swing.GroupLayout(panelBotoes);
        panelBotoes.setLayout(panelBotoesLayout);
        panelBotoesLayout.setHorizontalGroup(
            panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonContatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonEventos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 151, Short.MAX_VALUE)
                .addComponent(buttonSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panelBotoesLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonContatos, buttonEventos, buttonHome, buttonPerfil, buttonSair});

        panelBotoesLayout.setVerticalGroup(
            panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBotoesLayout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addGroup(panelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonContatos, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEventos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelBotoesLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonContatos, buttonEventos, buttonHome, buttonPerfil, buttonSair});

        panelBody.setBackground(new java.awt.Color(255, 255, 255));
        panelBody.setPreferredSize(new java.awt.Dimension(800, 500));
        panelBody.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                panelBodyComponentShown(evt);
            }
        });
        panelBody.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelBody, javax.swing.GroupLayout.DEFAULT_SIZE, 850, Short.MAX_VALUE)
            .addComponent(panelBotoes, javax.swing.GroupLayout.DEFAULT_SIZE, 850, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBody, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonPerfilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPerfilActionPerformed
        java.awt.CardLayout layout = (java.awt.CardLayout) panelBody.getLayout();
        layout.show(panelBody, "Perfil");
    }//GEN-LAST:event_buttonPerfilActionPerformed

    private void panelBodyComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_panelBodyComponentShown
        // TODO add your handling code here:
    }//GEN-LAST:event_panelBodyComponentShown

    private void buttonContatosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonContatosActionPerformed
        java.awt.CardLayout layout = (java.awt.CardLayout) panelBody.getLayout();
        layout.show(panelBody, "Contatos");
    }//GEN-LAST:event_buttonContatosActionPerformed

    private void buttonEventosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEventosActionPerformed
        java.awt.CardLayout layout = (java.awt.CardLayout) panelBody.getLayout();
        layout.show(panelBody, "Eventos");
    }//GEN-LAST:event_buttonEventosActionPerformed

    private void buttonSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSairActionPerformed

        try {
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(arquivo)));
            out.reset();
            out.writeObject(user);
            out.close();

        } catch (IOException i) {
            System.out.println("guia no out.");
            //Hope this doesn't happen.
        }

        System.exit(0);

    }//GEN-LAST:event_buttonSairActionPerformed

    private void buttonHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonHomeActionPerformed
        java.awt.CardLayout layout = (java.awt.CardLayout) panelBody.getLayout();
        layout.show(panelBody, "TelaInicial");
    }//GEN-LAST:event_buttonHomeActionPerformed

    public static Usuario setUsuario() {

        ObjectInputStream in = null;
        Usuario user = null;

        try {
            in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(arquivo)));
            try {
                user = (Usuario)in.readObject();
            } catch (ClassNotFoundException c) {
                System.exit(1);
            }
            in.close();
        } catch (FileNotFoundException f) {
            user = new Usuario();
        } catch (IOException i) {
            user = new Usuario();
        }
        return user;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        user = setUsuario();

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Body.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Body.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Body.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Body.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Body().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonContatos;
    private javax.swing.JButton buttonEventos;
    private javax.swing.JButton buttonHome;
    private javax.swing.JButton buttonPerfil;
    private javax.swing.JButton buttonSair;
    public static javax.swing.JPanel panelBody;
    private javax.swing.JPanel panelBotoes;
    // End of variables declaration//GEN-END:variables
}
